package cn.itcast.nems.common.tenant;

import java.util.Optional;

/**
 * author: yong.li
 * create: 2023-08-18 16:47:21
 **/
public class TenantInfoHolder {
    private static ThreadLocal<TenantInfo> holder = new ThreadLocal<>();

    public static void setDefaultTenantInfo() {
        holder.set(new TenantInfo(TenantInfo.DEFAULT_TENANT_ID, TenantInfo.DEFAULT_SUBTENANT_ID));
    }

    public static void setTenantInfo(TenantInfo tenantInfo) {
        holder.set(tenantInfo);
    }

    public static Optional<TenantInfo> getTenantInfo() {
        return Optional.ofNullable(holder.get());
    }

    public static TenantInfo getRequiredTenantInfo() {
        return getTenantInfo().orElseThrow(() -> new RuntimeException("没有找到租户相关信息"));
    }

    public static void clear() {
        holder.remove();
    }
}
